package com.zh.juc.locksupport;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: zhanghuan
 * @date: 2022/10/11 20:57
 * @description: create it
 * <p>
 * <p>
 * Condition condition = new ReentrantLock().newCondition();
 * condition.await();
 * condition.signal();
 * <p>
 * object和Condition使用的限制条件
 * 线程需要先获取锁并持有锁，必须在锁块（synchronized或lock）
 * 必须先等待后唤醒
 * <p>
 * LockSupport 使用Permit的概念做到阻塞和唤醒功能，每个线程都有一个许可,许可证最多就一个
 * park() unpark()
 * LockSupport 支持先唤醒再等待
 */
public class LockSupportDemo {

    public static void main(String[] args) {

        // syncWaitNotify();
        // lockAwaitSignal();

        Thread t1 = new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "\t-------come in");
            LockSupport.park();
            System.out.println(Thread.currentThread().getName() + "\t-------被唤醒");
        }, "t1");
        t1.start();


        // 现在让t2先执行
        new Thread(() -> {
            LockSupport.unpark(t1);
            System.out.println(Thread.currentThread().getName() + "\t-------发出通知");
        }, "t2").start();


    }

    private static void lockAwaitSignal() {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        // 线程t1
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName() + "\t-------come in");
                condition.await();
                System.out.println(Thread.currentThread().getName() + "\t-------被唤醒");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }

        }, "t1").start();

        // 线程t2 , 如果让线程2先执行会怎样？
        // 先signal后await线程是无法被唤醒的
        new Thread(() -> {
            lock.lock();
            try {
                condition.signal();
                System.out.println(Thread.currentThread().getName() + "\t-------发出通知");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }

        }, "t2").start();
    }

    private static void syncWaitNotify() {
        Object objectLock = new Object();
        // 线程1
        new Thread(() -> {
            //
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (objectLock) {
                System.out.println(Thread.currentThread().getName() + "\t-------come in");
                try {
                    objectLock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "\t-------被唤醒");
            }
        }, "t1").start();


        // 线程2， 如果线程2先执行就会卡住
        new Thread(() -> {
            synchronized (objectLock) {
                objectLock.notify();
                System.out.println(Thread.currentThread().getName() + "\t-------发出通知");
            }
        }, "t2").start();
    }

}
